home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / libtiff / tif_thunder.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  150 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_thunder.c,v 1.21 92/10/21 13:42:20 sam Rel $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  *
  32.  * ThunderScan 4-bit Compression Algorithm Support
  33.  */
  34. #include "tiffiop.h"
  35.  
  36. /*
  37.  * ThunderScan uses an encoding scheme designed for
  38.  * 4-bit pixel values.  Data is encoded in bytes, with
  39.  * each byte split into a 2-bit code word and a 6-bit
  40.  * data value.  The encoding gives raw data, runs of
  41.  * pixels, or pixel values encoded as a delta from the
  42.  * previous pixel value.  For the latter, either 2-bit
  43.  * or 3-bit delta values are used, with the deltas packed
  44.  * into a single byte.
  45.  */
  46. #define    THUNDER_DATA        0x3f    /* mask for 6-bit data */
  47. #define    THUNDER_CODE        0xc0    /* mask for 2-bit code word */
  48. /* code values */
  49. #define    THUNDER_RUN        0x00    /* run of pixels w/ encoded count */
  50. #define    THUNDER_2BITDELTAS    0x40    /* 3 pixels w/ encoded 2-bit deltas */
  51. #define        DELTA2_SKIP        2    /* skip code for 2-bit deltas */
  52. #define    THUNDER_3BITDELTAS    0x80    /* 2 pixels w/ encoded 3-bit deltas */
  53. #define        DELTA3_SKIP        4    /* skip code for 3-bit deltas */
  54. #define    THUNDER_RAW        0xc0    /* raw data encoded */
  55.  
  56. static const int twobitdeltas[4] = { 0, 1, 0, -1 };
  57. static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
  58.  
  59. #define    SETPIXEL(op, v) { \
  60.     lastpixel = (v) & 0xf; \
  61.     if (npixels++ & 1) \
  62.         *op++ |= lastpixel; \
  63.     else \
  64.         op[0] = lastpixel << 4; \
  65. }
  66.  
  67. static int
  68. DECLARE3(ThunderDecode, TIFF*, tif, register u_char*, op, u_long, maxpixels)
  69. {
  70.     register u_char *bp;
  71.     register int n, cc, delta;
  72.     register u_int lastpixel;
  73.     register u_long npixels;
  74.  
  75.     bp = (u_char *)tif->tif_rawcp;
  76.     cc = tif->tif_rawcc;
  77.     lastpixel = npixels = 0;
  78.     while (cc > 0 && npixels < maxpixels) {
  79.         n = *bp++, cc--;
  80.         switch (n & THUNDER_CODE) {
  81.         case THUNDER_RUN:        /* pixel run */
  82.             /*
  83.              * Replicate the last pixel n times,
  84.              * where n is the lower-order 6 bits.
  85.              */
  86.             if (npixels & 1) {
  87.                 op[0] |= lastpixel;
  88.                 lastpixel = *op++; npixels++; n--;
  89.             } else
  90.                 lastpixel |= lastpixel << 4;
  91.             npixels += n;
  92.             for (; n > 0; n -= 2)
  93.                 *op++ = lastpixel;
  94.             if (n == -1)
  95.                 *--op &= 0xf0;
  96.             lastpixel &= 0xf;
  97.             break;
  98.         case THUNDER_2BITDELTAS:    /* 2-bit deltas */
  99.             if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
  100.                 SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  101.             if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
  102.                 SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  103.             if ((delta = (n & 3)) != DELTA2_SKIP)
  104.                 SETPIXEL(op, lastpixel + twobitdeltas[delta]);
  105.             break;
  106.         case THUNDER_3BITDELTAS:    /* 3-bit deltas */
  107.             if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
  108.                 SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  109.             if ((delta = (n & 7)) != DELTA3_SKIP)
  110.                 SETPIXEL(op, lastpixel + threebitdeltas[delta]);
  111.             break;
  112.         case THUNDER_RAW:        /* raw data */
  113.             SETPIXEL(op, n);
  114.             break;
  115.         }
  116.     }
  117.     tif->tif_rawcp = (char *)bp;
  118.     tif->tif_rawcc = cc;
  119.     if (npixels != maxpixels) {
  120.         TIFFError(tif->tif_name,
  121.             "ThunderDecode: %s data at scanline %ld (%lu != %lu)",
  122.             npixels < maxpixels ? "Not enough" : "Too much",
  123.             tif->tif_row, npixels, maxpixels);
  124.         return (0);
  125.     }
  126.     return (1);
  127. }
  128.  
  129. static int
  130. DECLARE4(ThunderDecodeRow, TIFF*, tif, u_char*, buf, u_long, occ, u_int, s)
  131. {
  132.     u_char *row = buf;
  133.     
  134.     while ((long)occ > 0) {
  135.         if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
  136.             return (0);
  137.         occ -= tif->tif_scanlinesize;
  138.         row += tif->tif_scanlinesize;
  139.     }
  140.     return (1);
  141. }
  142.  
  143. int
  144. DECLARE1(TIFFInitThunderScan, TIFF*, tif)
  145. {
  146.     tif->tif_decoderow = ThunderDecodeRow;
  147.     tif->tif_decodestrip = ThunderDecodeRow;
  148.     return (1);
  149. }
  150.